<?php
use entities\IM_Admin_user;
class IM_C_ManageUsers extends BaseController{
	public function start(){
		fw_util_custom_doLoginCheck();
		//$this->registry->template->s_mainAppDisplay = $o_loginForm->getFormHtml();
		$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
		$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
		$this->registry->template->s_mainAppDisplay = $this->getUserTable();
		$this->getSubNavi();
		$this->registry->template->show('Admin-manageUsers.php');
	}
	
	public function addUser(){
		$o_addUserForm = new ClaycodeForm('add-user-form');
		$this->registry->template->s_mainAppDisplay = '<div class="form-container">' . $o_addUserForm->getFormHtml() . '</div>';
		$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
		$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
		//$this->registry->template->s_mainAppDisplay = 'test add user';
		$this->getSubNavi();
		$this->registry->template->show('Admin-manageUsers.php');
	}
	
	public function handleAddUserSubmit(){
		$o_addUserForm = new ClaycodeForm('add-user-form');
		$o_addUserForm->validateForm();
		if(count($o_addUserForm->getFormErrors()) == 0){
			$arr_inputVals = $o_addUserForm->getInputValues();
			
			$o_newUser = new entities\IM_Admin_user();
			$o_newUser->setUsername($arr_inputVals['username']);
			$o_newUser->setPassword($arr_inputVals['user-password']);
			
			//persist the new user
			$this->o_em->persist($o_newUser);
			$this->o_em->flush();
			
			fw_util_redirect('AdminManageUsers');
		}else{
			$this->registry->template->s_mainAppDisplay = '<div class="form-container">' . $o_addUserForm->getFormHtml() . '</div>';
			$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
			$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
			//$this->registry->template->s_mainAppDisplay = 'test add user';
			$this->getSubNavi();
			$this->registry->template->show('Admin-manageUsers.php');
		}
	}
	
	private function getUserTable(){
		$arr_appConf = Claycode_configuration::getInstance()->getAppConfByIdent('AdminAddUser');
		
		$s_addNewUserUrl = fw_util_getAbsoluteUrl() . $arr_appConf['controller_class_alias'] . '/' . $arr_appConf['action'];
		$s_addNewUserLink = '<a href="'. $s_addNewUserUrl . '">add new user</a>';
		
		
		$s_userTable = '<table id="users-table">';
		
		$s_userTable .= '<thead>';
		
		$s_userTable .= '<tr>';
		$s_userTable .= '<th colspan="2">&nbsp;</th>';
		$s_userTable .= '<th>'.$s_addNewUserLink.'</th>';
		$s_userTable .= '</tr>';
		
		$s_userTable .= '<tr>';
		$s_userTable .= '<th>&nbsp;</th>';
		$s_userTable .= '<th>username</th>';
		$s_userTable .= '<th>remove?</th>';
		$s_userTable .= '</tr>';
		
		$s_userTable .= '</thead>';
		
		$s_userTable .= '<tbody>';
		$s_userTable .= $this->getUsersRows();
		$s_userTable .= '</tbody>';
		
		$s_userTable .= '</table>';
		
		return $s_userTable;
	}
	
	private function getUsersRows(){
		$s_userRows = '';
		$arr_o_users = $this->o_em->getRepository('entities\IM_Admin_user')->getUsers();
		$arr_appConf = Claycode_configuration::getInstance()->getAppConfByIdent('AdminDeleteUser');
		
		if(!empty($arr_o_users)){
			$i_count = 1;
			foreach($arr_o_users as $o_user){
				$s_userId = $o_user->getId();
				$s_userName = $o_user->getUsername();
				$s_encryptedUserId = EncryptionHandler::get()->doEncrypt($s_userId);
				$s_deleteUserUrl = fw_util_getAbsoluteUrl() . $arr_appConf['controller_class_alias'] . '/' . $arr_appConf['action'] . '/user/' . $s_encryptedUserId;
				$s_deleteUserLink = '<a href="'. $s_deleteUserUrl . '">remove</a>';
				
				$s_userRows .= '<tr>';
					$s_userRows .= '<td>' . $i_count . '</td>';
					$s_userRows .= '<td>' . $s_userName . '</td>';
					$s_userRows .= '<td>' . $s_deleteUserLink . '</td>';
				$s_userRows .= '</tr>';
				$i_count++;
			}
		}else{
			$s_userRows = '<tr><td style="text-align:center;" colspan="3">no users yet</td></tr>';
		}
		
		return $s_userRows;
	}
	
	public function removeUser(){
		if(isset($_GET['user'])){
			if($_GET['user'] != '' || $_GET['user'] != null){
				$s_decryptedUserId = EncryptionHandler::get()->doDecrypt( $_GET['user']);
				
				$o_user = $this->o_em->find('entities\IM_Admin_user', (int)$s_decryptedUserId);
				
				try {
					$this->o_em->remove($o_user);
					$this->o_em->flush();
				} catch (Exception $e) {
					fw_util_redirect('AdminManageUsers');
				}
				
				fw_util_redirect('AdminManageUsers');
			}else{
				fw_util_redirect('AdminManageUsers');
			}
		}else{
			fw_util_redirect('AdminManageUsers');
		}
		
	}
}